package com.eshop.web.controllers.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.eshop.frameworks.core.controller.BaseController;
import com.eshop.model.mongodb.ECartItem;
import com.eshop.model.mongodb.EShop;
import com.eshop.model.mongodb.ShopAndGoods;
import com.eshop.service.mongodb.ECartItemService;
import com.eshop.service.mongodb.EShopService;
import com.eshop.service.mongodb.EUserService;
import com.eshop.service.mongodb.ShopAndGoodsService;
import com.eshop.web.controllers.web.EUserController;


@Controller
@RequestMapping("/api")
public class GoodsCarApiController extends BaseController{
	private static final Logger logger = Logger.getLogger(EUserController.class);
	
	@Autowired
	private EUserService euserService;
	
	@Autowired
	private ECartItemService ecartItemService;
	
	@Autowired
	private EShopService eshopService;
	
	@Autowired
	private ShopAndGoodsService shopAndGoodsService;
	
	@RequestMapping(value = "/addGoodsCar", method = RequestMethod.POST)
	@ResponseBody
	public String addCart(ECartItem ecartItem,HttpServletRequest request, HttpServletResponse response) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("success", true);
		try {
			ECartItem myItem = ecartItemService.getMyItem(ecartItem);
			if (myItem != null) {
				ecartItemService.addCount(myItem.getId());
			} else {
				ecartItem.setAddTime(new Date());
				ecartItem.setUserId(ecartItem.getUserId());
				ecartItem.setGoodsCount(1);
				ecartItem.setStatus(0);
				ecartItemService.save(ecartItem);
			}
		} catch (Exception e) {
			jsonObject.put("success", false);
			logger.error("EUserController.addCart", e);
		}
		jsonObject.put("timeStamp", System.currentTimeMillis());
		return jsonObject.toJSONString();
	}
	
	
	@RequestMapping("/getGoodsCar")
	@ResponseBody
	public String cart(String userId,HttpServletRequest request, HttpServletResponse response) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("success", true);
		Map<String, List<ECartItem>> itemMap = getItemsMap(userId);
		jsonObject.put("resluts", itemMap);
		jsonObject.put("timeStamp", System.currentTimeMillis());
		return jsonObject.toJSONString();
	}

	@RequestMapping("/deleteCarItem")
	@ResponseBody
	public String deleteCartItem(String itemId,HttpServletRequest request,HttpServletResponse response) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("success", true);
		ECartItem ecartItem = ecartItemService.findById(itemId,ECartItem.class);
		ecartItemService.remove(ecartItem);
		jsonObject.put("timeStamp", System.currentTimeMillis());
		return jsonObject.toJSONString();
	}
	
	
	private Map<String, List<ECartItem>> getItemsMap(String userId) {
		Map<String, List<ECartItem>> ecartMap = new HashMap<String, List<ECartItem>>();
		List<ECartItem> ecartItems = ecartItemService.getItems(userId);
		ShopAndGoods goods = null;
		EShop eshop = null;
		for (ECartItem item : ecartItems) {
			goods = shopAndGoodsService.findById(item.getGoodsId(),ShopAndGoods.class);
			item.setGoods(goods);
			eshop = eshopService.getEShopByUser(item.getShopId());
			item.setEshop(eshop);
			if (!ecartMap.containsKey(eshop.getShopName())) {
				ecartMap.put(eshop.getShopName(), new ArrayList<ECartItem>());
			}
			ecartMap.get(eshop.getShopName()).add(item);
		}
		return ecartMap;
	}
}
